var fss = {}

fss.Dialog = function (options)
{
    this._i = ++fss.Dialog._index;
    this.options = options;

    if (options.unique) {
        // Only keep one open dialog
        fss.Dialog.closeAll();
    }
    fss.Dialog._stack.push(this);

    if (options.message)
        this.content = $('<span>'+options.message+'</span>');
    else if (options.content)
        this.content = options.content;

    // Only keep one open dialog per content element
    if (this.content) {
        if (this.content.data('fss_dialog'))
            this.content.data('fss_dialog').remove();
        this.content.data('fss_dialog', this);
    }

    if (options.onconfirm)
        this.onconfirm = options.onconfirm;
    if (options.oncancel)
        this.oncancel = options.oncancel;

    if (options.closeOnConfirm !== undefined)
        this.closeOnConfirm = options.closeOnConfirm;
    else
        this.closeOnConfirm = true;

    if (options.closeOnCancel !== undefined)
        this.closeOnCancel = options.closeOnCancel;
    else
        this.closeOnCancel = true;

    this._create();
}

fss.Dialog._stack = $([]);
fss.Dialog._index = 0;

fss.Dialog.prototype.onconfirm = function () {}
fss.Dialog.prototype.oncancel = function () {}

fss.Dialog.prototype._create = function ()
{
    this.dialog_root = document.createElement('div');
    var content_outer = $('<div class="content_outer" />').appendTo($(this.dialog_root));
    if (this.options.title) {
        if (this.options.secure)
            var title_class = 'title secure';
        else
            var title_class = 'title';
        this.title = $('<h2 class="'+title_class+'">'+this.options.title+'</h2>');
        $(content_outer).append(this.title);
    }
    var content = $('<div class="content" />').appendTo(content_outer);
    var body = $('<div class="body"></div>').appendTo(content);
    if (this.content) {
        this.content_parent = this.content.parent();
        this.content.appendTo(body);
    }

    if (this.options.context) {
        var anchor_offset = $(this.options.context).offset();
        var link_height = $(this.options.context).outerHeight();
        var half_link_width = $(this.options.context).outerWidth() / 2;
        /* If change the default width */
        var dialog_width = 0;
        if (this.options.width > 0)
            dialog_width = this.options.width;
        else
            dialog_width = 467;
        /* Know if the link is on the right or on the left of the screen */
        if (anchor_offset.left > 379 || this.options.align == 'right') {
            var move_left = 415 - half_link_width - 467 + dialog_width; /* Have to function in iFrame */
            content.addClass('text-right');
        } else {
            var move_left = 0;
            content.removeClass('text-right');
        }
        this.context_dialog = $('<div class="context_dialog"></div>').appendTo($('body'))
            .append($(this.dialog_root))
            .css({
                 top: (anchor_offset.top + link_height + 2) + 'px',
                 left: (anchor_offset.left - move_left) + 'px',
                 width: dialog_width + 'px'
            });
        var arrow = $('<div class="arrow"></div>').appendTo(this.context_dialog)
            .css({
                 left: (move_left + half_link_width) + 'px'
            });
    } else {
        fb_opts = {
            content: this.dialog_root,
            loader:  false,
            visible: false
        };
        if (this.options.width)
            fb_opts.width = this.options.width;
        else
            fb_opts.width = 450;
        if (this.options.closeIcon)
            fb_opts.closeIcon = this.options.closeIcon;
        else if (!this.options.confirm && !this.options.cancel)
            fb_opts.closeIcon = true;
        if (fb_opts.closeIcon && this.options.oncancel)
            fb_opts.onClose = this.options.oncancel;
        this.fb_dialog = FB.Dialog.create(fb_opts);
    }

    if(this.options.littleClose) {
        this.buttons = $('<div class="littleClose" />').appendTo(content);
        this.addLittleCloseButton();
    } else {
        this.buttons = $('<div class="buttons" />').appendTo(content);

        if (this.options.confirm)
            this.addConfirmButton(this.options.confirm);
        if (this.options.cancel)
            this.addCancelButton(this.options.cancel);
    }

    if (this.content)
        this.content.show(0, function()
    {
        var tmp= $(this).offset(); // forces IE6 to refresh after DOM insertion
    });
    
    this.show();
}

fss.Dialog.prototype.addButton = function(name, text, onClick, classname, before)
{
    var button = $('<input />', {
        type: 'button',
        name: name,
        value: text,
        click: onClick
    });
    if (before) {
        button.insertBefore($('input[name="'+before+'"]', this.buttons));
    } else
        button.appendTo(this.buttons);
    if (classname)
        button.attr('class', classname);
    else
        button.attr('class', 'fss_button');
}

fss.Dialog.prototype.removeButton = function(name)
{
    $('input[name="'+name+'"]', this.buttons).remove();
}

fss.Dialog.prototype.addConfirmButton = function(text, before)
{
    var dialog = this; // .bind() doesn't work here, why?
    this.addButton('confirm_button', text, function () {
        if (dialog.closeOnConfirm)
            dialog.remove();
        dialog.onconfirm();
    }, 'vtns_button vtns_buttonBlue', before);
}

fss.Dialog.prototype.addCancelButton = function(text, before)
{
    var dialog = this; // .bind() doesn't work here, why?
    this.addButton('cancel_button', text, function () {
        if (dialog.closeOnCancel)
            dialog.remove();
        dialog.oncancel();
    }, 'vtns_button', before);
}

fss.Dialog.prototype.addLittleCloseButton = function()
{
    var dialog = this; // .bind() doesn't work here, why?
    var button = $('<a />', {
        href: '#',
        click: function () {
         dialog.remove();
         return false;
        }
    });
    button.appendTo(this.buttons);
    var icon = $('<i />');
    icon.addClass('FB_icons icon_delete');
    icon.appendTo(button);
}

fss.Dialog.prototype.show = function ()
{
    if (this.fb_dialog) {
        FB.Dialog.show(this.fb_dialog);
    } else {
        this.context_dialog.show();
    }
}

fss.Dialog.prototype.remove = function ()
{
    if (this.fb_dialog)
        FB.Dialog.remove(this.fb_dialog);
    else if (this.context_dialog)
        this.context_dialog.hide();
    if (this.content_parent)
        this.content.hide().appendTo(this.content_parent);
    if (this.context_dialog)
        this.context_dialog.remove();
    if (this.content)
        this.content.removeData('fss_dialog', this);
    var dialog = this;
    fss.Dialog._stack = fss.Dialog._stack.filter(function() {
        return this !== dialog;
    });
}

fss.Dialog.closeAll = function()
{
    fss.Dialog._stack.each(function () {
        this.remove();
        this.oncancel();
    });
}
